package com.beidu.lottery.call.service.portal.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beidu.lottery.call.dao.MQMessageLogMapper;
import com.beidu.lottery.call.entity.MQMessageLog;
import com.beidu.lottery.call.entity.mq.ActivityBaseDTO;
import com.beidu.lottery.call.service.portal.MQMessageLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * MQ消费记录表 服务实现类
 * </p>
 *
 * @author 北渡
 * @since 2023-10-17
 */
@Service
public class MQMessageLogServiceImpl extends ServiceImpl<MQMessageLogMapper, MQMessageLog> implements MQMessageLogService {

    @Override
    public void saveOrUpdate(String topic, JSONObject data, boolean isSuccess) {
        // 活动DTO
        ActivityBaseDTO dto = data.getObject("dto", ActivityBaseDTO.class);
        // 组装MQ消费记录Entity
        MQMessageLog entity = MQMessageLog.builder()
                .id(dto.getMessageId())
                .topic(topic)
                .body(data.toJSONString())
                .status(isSuccess ? "1" : "0").build();
        // 查询记录
        MQMessageLog row = this.getById(entity.getId());
        // 保存或更新记录
        if (row == null) {
            this.save(entity);
        } else {
            entity.setUpdatedDate(LocalDateTime.now());
            this.updateById(entity);
        }
    }

}
